import 'package:flutter/material.dart';

import 'self_adaption_widgets.dart';

// 自定义的一个带有图标和文字的按钮
class IconTextButton extends StatefulWidget {
  final String value; // 数值
  final IconData icon; // 图标
  final VoidCallback onPressed; // 回调函数
  final double height; // 高度

  const IconTextButton({
    super.key,
    required this.value,
    required this.icon,
    required this.onPressed,
    this.height = 18,
  });

  @override
  State<IconTextButton> createState() => _IconTextButtonState();
}

class _IconTextButtonState extends State<IconTextButton> {
  bool _isHighlighted = false; // 按钮是否被点击

  @override
  Widget build(BuildContext context) {
    int? numValue = int.tryParse(widget.value);
    String num = numValue != null && numValue > 999 ? '999+' : widget.value;

    return Row(
      children: [
        GestureDetector(
          onTapDown: (_) {
            setState(() {
              _isHighlighted = true;
            });
          },
          onTapUp: (_) {
            // 抬起时取消高亮
            setState(() {
              _isHighlighted = false;
            });
            // 调用父组件的回调
            widget.onPressed();
          },
          onTapCancel: () {
            // 取消时取消高亮
            setState(() {
              _isHighlighted = false;
            });
          },
          child: Container(
            decoration: BoxDecoration(
              color: _isHighlighted
                  ? Colors.grey.withOpacity(0.5)
                  : Colors.transparent,
              borderRadius: BorderRadius.circular(widget.height),
            ),
            child: Icon(
              widget.icon,
              size: widget.height,
            ),
          ),
        ),
        // 按钮的数字部分

        SelfAdaptionText.fitHeight(
          num,
          height: widget.height,
          // width: textWidth,
        ),
      ],
    );
  }
}
